package com.front.web.lvyouXcxWeb;

import com.bootdo.common.utils.*;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.haojiangboutils.ResultReBuild;
import com.bootdo.lvyou.domain.LvyouScenicSpot;
import com.bootdo.lvyou.domain.LvyouScenicTicket;
import com.bootdo.lvyou.domain.LvyouScenicTicketMoney;
import com.bootdo.lvyou.domain.LvyouSpecialtyEnclosure;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/f/scenicspot/v1/")
@RestController
public class ScenicSpotControllerV1 extends LvyouSupperController{


    /**
     * 得到酒店列表
     * http://127.0.0.1/f/scenicspot/v1/get_scenic_spot_by_distance?lon=114.46353400029057&lat=36.62493730910808&pageNo=0&pageSize=5&sort=distance
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("get_scenic_spot_by_distance")
    public R getScenicSpotByDistance(
            Integer pageNo,
            Integer pageSize,
            Double lon,
            Double lat,
            String sort,
            Integer isRemen,
            String lables,
            Long  cityId,
            String name,
            HttpServletRequest request, HttpServletResponse response){
        Map query  = null;

        if(null == lon || null == lat){
            return R.error("请传入位置信息");
        }

        //如果不是测试调用  就转gps 偏移
        if(StringUtils.isEmpty(request.getParameter("b"))){
            //火星转百度09
            double[] gps =  GPSUtil.gcj02_To_Bd09(lat, lon);
            lat = gps[0];
            lon = gps[1];
        }

        if(StringUtils.isEmpty(sort)){
            sort = "distance";
        }
        if(null != pageNo && null != pageSize){
              query =  MapBuild.getInstand().setKV("pageNo",pageNo*pageSize)
                    .setKV("pageSize", pageSize)
                    .setKV("lon", lon)
                    .setKV("lat", lat)
                    .setKV("sort", sort)
                    .setKV("lables", lables)
                    .setKV("name", name)
                    .setKV("isRemen", isRemen)
                     .setKV("cityId", cityId);
        }else{
            query =  MapBuild.getInstand()
                    .setKV("lon", lon)
                    .setKV("lat", lat)
                    .setKV("sort", sort)
                    .setKV("lables", lables)
                    .setKV("isRemen", isRemen)
                    .setKV("name", name)
                    .setKV("cityId", cityId);
        }

        if(null != pageNo){
            //总记录数
            int rows= lvyouScenicSpotService.getByLonLatCount(query);
            int pageSum=(rows-1)/pageSize+1;

            if(pageNo > pageSum-1){
                return R.error("没有更多了");
            }
        }



        List<LvyouScenicSpot> l =  lvyouScenicSpotService.getByLonLat(
                query
        );
        if(null != isRemen && isRemen.intValue() == 1){
            l.stream().forEach(item->{
                //item.getId();
                LvyouScenicSpot  t = lvyouScenicSpotService.get(item.getId());
                if(StringUtils.isNotEmpty(t.getIntroduce())){
                    Document document =  Jsoup.parse(t.getIntroduce());
                    if(document.text().length() > 200){
                        item.setTraffic(document.text().substring(0, 199));
                    }else{
                        item.setTraffic(document.text());
                    }
                }
            });
        }
        return R.ok().put("l",l);
    }


    /**
     * 得到景点富文本
     * http://127.0.0.1/f/scenicspot/v1/get_scenic_spot_detail?spotId=1
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("get_scenic_spot_detail")
    public R getScenicSpotTraffic(
             @RequestParam("spotId") Long spotId,
            HttpServletRequest request, HttpServletResponse response){
        LvyouScenicSpot lvyouScenicSpot  = lvyouScenicSpotService.get(spotId);
        return R.ok().put("i",lvyouScenicSpot);
    }



    /**
     * 得到景点轮播图
     * http://127.0.0.1:8090/f/scenicspot/v1/get_scenic_spot_by_pics?spotId=2
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("get_scenic_spot_by_pics")
    public R getScenicSpotPics(
            Long spotId,
            HttpServletRequest request, HttpServletResponse response){
        List<LvyouSpecialtyEnclosure> l =   lvyouSpecialtyEnclosureService
                .list(MapBuild.getInstand().setKV("keyStr",spotId).setKV("type", "2"));
        return R.ok().put("l", new ResultReBuild<LvyouSpecialtyEnclosure>().toNewObj(l, new String[]{
                "fileUrl"
        }));
    }

    /**
     *  http://127.0.0.1:8090/f/scenicspot/v1/get_scenic_tickets?spotId=2
     * 得到景点门票列表
     * @param spotId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("get_scenic_tickets")
    public R getScenicTickets(
            @RequestParam(required = true,defaultValue = "-1",name = "spotId") Long spotId,
            HttpServletRequest request, HttpServletResponse response){
        String newDate = DateUtils.format(new Date());
        List<LvyouScenicTicket> l =  lvyouScenicTicketService.list(MapBuild.getInstand().setKV("spotId",spotId));
        l.forEach(item ->{
            LvyouScenicTicketMoney lvyouScenicTicketMoney = lvyouScenicTicketMoneyService.getByNowDate(item.getId(),newDate);
            item.setMoney(lvyouScenicTicketMoney.getMoney());
        });
        return R.ok().put("l",l);
    }


    /**
     *  http://127.0.0.1:8090/f/scenicspot/v1/get_scenic_ticket_moneys?ticketId=1
     * 得到景点门票价格大对象
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("get_scenic_ticket_moneys")
    public R getScenicTicketMoneys(
            @RequestParam(required = true,defaultValue = "-1",name = "ticketId") Long ticketId,
            HttpServletRequest request, HttpServletResponse response){
        String newDate = DateUtils.format(new Date());
        Map<String,Double> resultMap = new HashMap();
        List<LvyouScenicTicketMoney> l  =  lvyouScenicTicketMoneyService
                .list(MapBuild.getInstand().setKV("ticketId",ticketId)
                .setKV("nowDateFm", newDate));
        l .forEach(item ->{
            resultMap.put(DateUtils.format(item.getNowDate(),
                    DateUtils.DATE_PATTERN),item.getMoney());
        });
        return R.ok().put("i",resultMap);
    }

}
